package com.ihai.spring.boot.sample.controller;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * DESC
 *
 * @author 张驰
 * @date 2019/12/12
 */
@SpringBootApplication
@RequestMapping("/test")
@RestController
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
    @RequestMapping("a/a/a")
    public Map<String,Object> get(HttpServletResponse response){
        response.setHeader("key","ddsdfsdf,Content-Type");
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","测试一下111");
        map.put("data",null);
        return map;
    }
    @RequestMapping("/query")
    public Map<String, String> query(@RequestParam(defaultValue = "1000") String size) {
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < Integer.parseInt(size); ) {
            map.put("key" + i, "value" + i);
            i += ("key" + i).length() + ("value" + i).length() + 6;
        }
        Random random = new Random();
        try {
            Thread.sleep(50+(int)(random.nextFloat()*100));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return map;
    }

    @Bean
    public TomcatServletWebServerFactory getEmbeddedServletContainerFactory() {
        TomcatServletWebServerFactory containerFactory = new TomcatServletWebServerFactory();
        containerFactory
                .addConnectorCustomizers(new TomcatConnectorCustomizer() {
                    @Override
                    public void customize(Connector connector) {
                        connector.setPort(8080);
                        connector.setProperty("connectionTimeout", "600000");
                        connector.setProperty("acceptorThreadCount", "50");
                        connector.setProperty("minSpareThreads", "100");
                        connector.setProperty("maxSpareThreads", "500");
                        connector.setProperty("maxThreads", "2000");
                        connector.setProperty("maxConnections", "50000");
                        connector.setProperty("redirectPort", "redirectPort");
                        connector.setProperty("compression", "compression");
                        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                        //设置最大连接数
                        protocol.setKeepAliveTimeout(120 * 1000);
                        protocol.setMaxKeepAliveRequests(1000);
                    }
                });

        return containerFactory;
    }
}
